<?php

/**
	@brief Returns a bunch of <option> tags corresponding to an enumeration
 */
function enum_list($ename, $selection = 0)
{
	global $g_tplvars;
	
	//First, find the enum
	$r = dbquery('select * from enums where `name` = \'' . $ename . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$e = mysql_fetch_object($r);
	
	//and then dump all of its data
	$r = dbquery('select * from enumvals where `eid` = \'' . $e->id . '\'');
	$ret = '';
	while($line = mysql_fetch_object($r))
	{
		if($line->val == $selection)
			$g_tplvars['selected'] = 'selected=\'1\'';
		else
			$g_tplvars['selected'] = '';
			
		$g_tplvars['value'] = $line->val;
		$g_tplvars['name'] = $line->name;
		$ret .= templatize('../templates/enum-selector-line.html');
	}
	return $ret;
}

/**
	@brief Converts an int to an enum name
	
	@param ename Enum name
	@param value Value
 */
function enum_value_to_name($ename, $value)
{
	//First, find the enum
	$r = dbquery('select * from enums where `name` = \'' . $ename . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$e = mysql_fetch_object($r);
	
	//and then dump the fun stuff
	$r = dbquery('select * from enumvals where `eid` = \'' . $e->id . '\' and `val` = \'' . $value . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$v = mysql_fetch_object($r);
	return $v->name;
}

/**
	@brief Converts an enum name to an int
	
	@param ename Enum name
	@param name Name of the value to conver
 */
function enum_name_to_value($ename, $name)
{
	//First, find the enum
	$r = dbquery('select * from enums where `name` = \'' . $ename . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$e = mysql_fetch_object($r);
	
	//and then dump the fun stuff
	$r = dbquery('select * from enumvals where `eid` = \'' . $e->id . '\' and `name` = \'' . $name . '\'');
	if(mysql_num_rows($r) != 1)
		return '';
	$v = mysql_fetch_object($r);
	return $v->val;
}

?>
